package server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;

public class GetTrackIp extends Thread{
	String ip;
	public GetTrackIp(String ip)
	{
		this.ip=ip;
		String content = "";
		try {

			Random random = new Random();
			List<String> listSock = new ArrayList<String>();
			listSock.add("184.107.74.40");
			listSock.add("184.107.74.41");
			listSock.add("184.107.74.42");
			listSock.add("184.107.74.43");
			listSock.add("184.107.74.44");
			listSock.add("184.107.74.45");
			listSock.add("184.107.74.46");
			listSock.add("184.107.74.47");
			int rand = new Random().nextInt(7);
			Socket sockket = new Socket("whatismyipaddress.com", 80, InetAddress.getByName(listSock.get(rand)), random.nextInt(1000)+30000);
//			Socket sockket = new Socket("whatismyipaddress.com", 80);
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(sockket.getOutputStream()));
			
			writer.write("GET /ip/"+ip+" HTTP/1.1\r\n");
			writer.write("Host: whatismyipaddress.com\r\n");
			writer.write("Referer: http://5ty.org\r\n");
			
			writer.write("User-Agent:	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1\r\n");
			writer.write("Connection:	Close\r\n");
			
			
			writer.write("\r\n\r\n");
			writer.flush();
			BufferedReader reader =  new BufferedReader(new InputStreamReader(sockket.getInputStream()));
			
			char[] buf = new char[102102];
			int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
			
			Document doc = Jsoup.parse(content);
	
			BasicDBObject info = new BasicDBObject();
			
			DBCollection coll = Data.data("ip");
			info.put("name", ip);
			try {
				info.put("jsp",doc.select("div[id=section_content]").select("table").get(0).select("td").get(1).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				info.put("organization",doc.select("div[id=section_content]").select("table").get(0).select("td").get(2).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				info.put("proxy",doc.select("div[id=section_content]").select("table").get(0).select("td").get(3).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				info.put("type",doc.select("div[id=section_content]").select("table").get(0).select("td").get(4).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				info.put("assignment",doc.select("div[id=section_content]").select("table").get(0).select("td").get(5).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				info.put("country",doc.select("div[id=section_content]").select("table").get(1).select("td").get(0).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				info.put("state",doc.select("div[id=section_content]").select("table").get(1).select("td").get(1).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				info.put("city",doc.select("div[id=section_content]").select("table").get(1).select("td").get(2).text());
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			coll.insert(info);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}

	public static void main( String args[])
	{
		new GetTrackIp("50.17.209.163");
	}
}
